import {
  getItem,
  setItem
} from "../../utils/auth";
import {userProfile} from '@/api/user.js'
const state = {
  token: getItem() || '',
  userInfo:'',
  refresh_token:localStorage.getItem('refresh_token') || ''
}
const mutations = {
  setToken(state, value) {
    state.token = value
    setItem(value)
  },
  setUserInfo(state,value){
    state.userInfo=value
  },
  //修改userInfo的某个属性
  setUserOther(state,value){
state.userInfo[value.key]=value.value
  },
  //修改refresh_token
  setRefreshToken(state,value){
    state.refresh_token=value
    localStorage.setItem('refresh_token',value)
  }
}
const actions = {
  async getUserInfo(store){
    if(store.state.userInfo===''){
      const res =await userProfile()
      store.commit('setUserInfo',res.data)
      console.log(res);
    }
    
  }
}
const getters = {
    getToken(state){return state.token}
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
